package com.qfedu.openplateform.manager.controller;

import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.Customer;
import com.qfedu.openplateform.manager.entity.Menu;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: wmh
 * @Date:2021/12/2 19:30
 */
@RequestMapping("/sys/customer")
@RestController
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping( "/table")
    public TableData<Customer> list() {
        List<Customer> customers = customerService.getAllCustomer();
        System.out.println("-------------------");
        return new TableData<>(customers.size(), customers);
    }

    @RequestMapping( "/del")
    public AjaxMessage delete(Integer[] ids) {
        try {
            customerService.deleteCustomer(ids);
            return new AjaxMessage(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxMessage(true, "删除失败");
        }
    }

    @RequestMapping( "/add")
    public AjaxMessage add(Customer customer) {
        try {
            customerService.addCustomer(customer);
            return new AjaxMessage(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxMessage(true, "添加失败");
        }
    }

    @RequestMapping( "/update")
    public AjaxMessage update(Customer customer) {
        try {
            customerService.updateCustomer(customer);
            return new AjaxMessage(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxMessage(true, "修改失败");
        }
    }

    @RequestMapping("/tree")
    public List<Customer> treeCustomer(){

        return customerService.treeCustomer();

    }

}
